<?php
namespace app\article\controller;
use think\Request;
use app\common\controller\Base;

class Article extends Base
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
    }
    
    public function index()
    {
        $page = input('param.page');
        $id = input('param.id');
        $category = input('param.category');
        if (!$id) {
            $id = db($this->itemCategory)->where('url_name',$category)->find()['id'];
        }
        if (!$id) {
            $itemInfo['id'] = '';
            $itemInfo['title'] = $this->itemName;
            $itemInfo['seo_title'] = '';
            $itemInfo['keywords'] = '';
            $itemInfo['description'] = '';
            $itemInfo['url_name'] = $this->itemType;
            $itemInfo['url'] = $this->domain . '/' . $this->itemType . '/';
        }
        if ($id) {
            $itemInfo = model($this->itemCategoryModelNameSpace)->getCategoryInfo($id);
            if (!$itemInfo) {
                throw new \think\exception\HttpException(404, '分类不存在');
            }
        }
        
        $this->assign('page',$page ? $page : 1);
        $this->assign('categoryUrlName',$itemInfo['url_name']);
        $this->assign('itemDetailId',$itemInfo['id']);
        
        $itemInfo['cid'] = $itemInfo['id'];
        
        $itemInfo['mipContent'] = model('app\common\model\Common')->getContentFilterByContent($itemInfo['content']);
        $this->assign('itemInfo',$itemInfo);
        
        $siteTitle = $itemInfo['seo_title'] ? $itemInfo['seo_title'] : $itemInfo['title'] . $pageText . $this->settingInfo['titleSeparator'] . $this->settingInfo['siteName'];
        $this->assign('siteTitle', $siteTitle);
        $this->assign('siteKeywords',$itemInfo['keywords']);
        $this->assign('siteDescription',$itemInfo['description']);
        
        $templateName = $itemInfo['template'] ? $itemInfo['template'] : 'article';
        $templateName = str_replace('.html', '', $templateName);
        $itemInfo['model_name'] = $itemInfo['model_name'] ? $itemInfo['model_name'] : 'article';
        return $this->siteView($itemInfo['model_name'] . '/' . $templateName);
    }

}